package com.ecode.ecny.service;

import com.ecode.ecny.uitl.DateTimeUtil;
import org.springframework.stereotype.Service;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName GenserateService
 * @Author mjlft
 * @Date 2021/8/15 17:57
 * @Version 1.0
 * @Description 流水生成器
 */
@Service
public class GenerateService {

    private AtomicInteger atomicInteger = new AtomicInteger(0);
    private final static int SIGN_MAX_LEN = 99999;

    /**
     * ECNY_[SYS]_yyyyDDmmHHMMss_[xxxxx]
     *
     * @param reqSys
     * @return
     */
    public String generateSingNo(String reqSys) {
        String signNo = "ECNY_" + reqSys + "_" + DateTimeUtil.getCurrentDateTime();
        synchronized (GenerateService.class){
            int index = atomicInteger.incrementAndGet();
            if(index > SIGN_MAX_LEN){
                index = 0;
                atomicInteger.set(index);
            }
            signNo = signNo + "_" + String.format("%05d", index);
        }
        return signNo;
    }
}
